import Layer from '../../core/Layer';
import Rectangle from '../../base/Rectangle';

export default class extends Layer {
    constructor(canvas, style, data = []) {
        super(canvas, style);
        this.style = style;
        this.base = style.base || 0;
        this.data = data;
        this.count = style.count || this.data.length;
        this.shift = style.shift || 0;
        this.barHeight = style.barHeight || (this.height / this.count);
        this.xStep = style.xStep || 10;
        this.yStep = style.yStep || (this.height / this.count);
    }

    getMinMax() {
        let min = Number.MAX_VALUE;
        let max = Number.MIN_VALUE;
        let end = this.data.length - this.shift - 1;
        let start = end - this.count + 1;
        for (let i = start; i <= end; i++) {
            if (min > this.data[i]) {
                min = this.data[i];
            }
            if (max < this.data[i]) {
                max = this.data[i];
            }
        }
        this.max = max;
        this.min = min;
    }

    build(painter) {
        this.childs.splice(0);
        let end = this.data.length - this.shift - 1;
        let start = end - this.count + 1;
        console.log('width===>', this.width);
        for (let i = start; i <= end; i++) {
            let rect = new Rectangle(this.canvas, {
                ...this.style,
                width: Math.round(Math.abs(this.data[i]) * this.xStep),
                height: this.barHeight,
                left: this.data[i] > 0 ? this.base : this.base  - Math.abs(this.data[i]) * this.xStep,
                bottom: (i - start) * this.yStep,
            });
            this.addChild(rect);
        }
    }
}
